﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using JMT.Data;
using System.Windows;
using JMT.Data.Events;

namespace JMT.Controls
{
    public class JMSolutionTree : TreeView
    {
        Data.JSolution _solution;
        TreeViewItem tis;

        public JMSolutionTree()
        {
            AllowDrop = true;
        }

        protected override void OnMouseRightButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            object obj = this.InputHitTest(e.GetPosition(this));
            if (obj is TextBlock)
            {
                TextBlock block = obj as TextBlock;
                if (block.Tag != null && block.Tag is JTreeNode)
                {
                    TreeViewItem selItem = block.Tag as TreeViewItem;
                    selItem.IsSelected = true;
                    if (selItem.DataContext is JSolution)
                    {
                        ContextMenu menu = ContextMenus.ContextMenuFactory.Get("solutiontreenode");
                        menu.Tag = selItem.DataContext;
                        menu.DataContext = selItem.DataContext;
                        menu.IsOpen = true;
                    }
                    else if (selItem.DataContext is JClass)
                    {
                        ContextMenu menu = ContextMenus.ContextMenuFactory.Get("classtreenode");
                        menu.Tag = selItem.DataContext;
                        menu.DataContext = selItem.DataContext;
                        menu.IsOpen = true;
                    }
                    else if (selItem.DataContext is JPackage)
                    {
                        ContextMenu menu = ContextMenus.ContextMenuFactory.Get("packagetreenode");
                        menu.Tag = selItem.DataContext;
                        menu.DataContext = selItem.DataContext;
                        menu.IsOpen = true;
                    }
                }
            }
            base.OnMouseRightButtonDown(e);
        }

        protected override void OnSelectedItemChanged(System.Windows.RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem selItem = e.NewValue as TreeViewItem;
            if (selItem != null)
            {
                JUI.PropertyView.Bind(selItem.DataContext as JModel);
            }
            base.OnSelectedItemChanged(e);
        }

        protected override void OnMouseDoubleClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            object obj = this.InputHitTest(e.GetPosition(this));
            if (obj is TextBlock)
            {
                TextBlock block = obj as TextBlock;
                if (block.Tag != null && block.Tag is JTreeNode)
                {
                    TreeViewItem selItem = block.Tag as TreeViewItem;
                    selItem.IsSelected = true;
                    if (selItem.DataContext is JDiagram)
                    {
                        JUI.DiagramContainer.Show(_solution, selItem.DataContext as JDiagram);
                    }
                }
            }
            base.OnMouseDoubleClick(e);
        }

        /// <summary>
        /// 鼠标移动，判断是否可以进行节点拖动操作
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                object obj = this.InputHitTest(e.GetPosition(this));
                if (obj is TextBlock)
                {
                    TextBlock block = obj as TextBlock;
                    if (block.Tag != null && block.Tag is JTreeNode)
                    {
                        TreeViewItem selItem = this.SelectedItem as TreeViewItem;
                        if (selItem != null && (selItem.DataContext is JMT.Data.JClass || selItem.DataContext is JMT.Data.JInterface))
                        {
                            DragDropEffects finalDropEffect = DragDrop.DoDragDrop(selItem, selItem.DataContext, DragDropEffects.Copy);
                        }
                    }
                }
            }

            base.OnMouseMove(e);
        }

        #region get node
        public TreeViewItem GetNode(JModel jModel)
        {
            TreeViewItem bingoItem = null;
            foreach (TreeViewItem item in this.Items)
            {
                if (item.DataContext == jModel)
                {
                    bingoItem = item;
                    break;
                }

                if (bingoItem == null)
                {
                    bingoItem = GetNode(item, jModel);
                }

                if (bingoItem != null)
                {
                    break;
                }
            }
            return bingoItem;
        }

        public TreeViewItem GetNode(TreeViewItem pItem, JModel jModel)
        {
            TreeViewItem bingoItem = null;
            foreach (TreeViewItem item in pItem.Items)
            {
                if (item.DataContext == jModel)
                {
                    bingoItem = item;
                    break;
                }

                if (bingoItem == null)
                {
                    bingoItem = GetNode(item, jModel);
                }

                if (bingoItem != null)
                {
                    break;
                }
            }
            return bingoItem;
        }
        #endregion
    }
}
